package com.homegoods.designer.presenter;

import com.homegoods.designer.R;
import com.homegoods.designer.event.EditSpaceEvent;
import com.homegoods.designer.model.EditDesignCase2Model;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.mvp.BaseView;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 */
public class EditDesignCase2Presenter extends BasePresenter<BaseView> {

    private MultiTypeAdapter adapter = new MultiTypeAdapter();

    public EditDesignCase2Presenter(BaseView view) {
        super(view);
    }

    public MultiTypeAdapter getAdapter() {
        return adapter;
    }

    public void initData() {
        String[] defaultSpace = mView.getContext().getResources().getStringArray(R.array.designer_default_space);
        addSubscription(Observable.fromArray(defaultSpace)
                .map(this::createSpace)
                .toList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseItems -> adapter.addAllWithClear(baseItems))
        );
    }

    public void addSpace(String spaceName) {
        adapter.addItem(createSpace(spaceName));
        adapter.notifyItemInserted(adapter.getItemCount() - 1);
    }

    private BaseItem createSpace(String s) {
        EditDesignCase2Model model = new EditDesignCase2Model();
        model.setTitle(s);
        return model.createItem(adapter);
    }

    public void updateSpace(EditSpaceEvent ev) {
        int index = ev.index;
        EditDesignCase2Model model = ev.model;
        adapter.setItem(index, model.createItem(adapter));

    }
}